#include "StdAfx.h"
#include "dll_object.h"
#include <interface/aot_sound_card_interface.h>
#include "voip_svc_impl.h"

dll_object* g_dll_obj = NULL;

voip_driver* 
get_voip_driver()
{
	return g_dll_obj->voip_svc_impl_->voip_driver_;
}

dll_object::dll_object(void)
{
	imain_ = NULL;
	voip_svc_impl_ = new voip_svc_impl;
	plugins_reg_fini_ = false;
}

dll_object::~dll_object(void)
{
	if ( voip_svc_impl_ )
	{
		delete voip_svc_impl_;
		voip_svc_impl_ = NULL;
	}
}

bool dll_object::query_interface(void** out, const char* key)
{
	if( NULL == key )
		return false;

	if( 0 == strcmp(key, "ivoip_svc") )
	{
		*out = (aot::voip::ivoip_svc*)this->voip_svc_impl_;
		return true;
	}

	return false;
}

bool dll_object::on_regist(aot::tt::idll::dll_regist_param_t* p, aot::tt::result_t* ret /*= NULL*/)
{
	init_log(p->mi);
	this->imain_ = p->mi;

	if( !p->mi->query_interface(this->aot_inet_dll_, "idll.aot_inet.dll") )
	{
		aot_log_error(AOT_LM_ERROR, "find idll.aot_inet.dll failed");
		return false;
	}

	if( !this->voip_svc_impl_->init() )
	{
		aot_log_error(AOT_LM_ERROR, "voip_svc_impl_->init() failed");
		return false;
	}
	regist_sys_event(p);
	return true;
}

void 
dll_object::regist_sys_event(aot::tt::idll::dll_regist_param_t* p)
{
	int ev[] = 
	{
		WM_GLB_VOIP_TO_UI_SND_MSG,
	};

	aot::tt::imain::regist_sys_event_t t;
	t.event_type = aot::tt::imain::regist_sys_event_t::e_type_voip;
	t.event_lst = ev;
	t.event_num = sizeof(ev) / sizeof(ev[0]);
	t.token = p->token;
	this->imain_->regist_to((void*)&t, aot::tt::imain::e_regist_sys_event);
}

bool
dll_object::query_interface_from_aot_inet_dll(void** out, int key)
{
	*out = NULL;
	if( !this->aot_inet_dll_->query_interface_by_iid(out, key) || ( NULL == *out ) )
	{
		aot_log_error(AOT_LM_ERROR, "query_interface [iid = %d] from idll.aot_inet.dll failed", key);
		return false;
	}
	return true;
}

void 
dll_object::init_log(aot::tt::imain* imain_obj)
{
	aot::tt::ilog *log_obj = NULL;

	if(!imain_obj->query_interface((void**)&log_obj, "ilog"))
	{
		return;
	}

	aot_log_init(log_obj);
	aot_log_cfg_from_file("idll.aot_voip.dll", NULL);
	aot_log_debug(AOT_LM_DEBUG, "%s", "aot_voip log initial ok !");
}

int 
dll_object::on_sys_event(int event_type, int ev, WPARAM wparam, LPARAM lparam, aot::tt::sys_event_param_t* sep)
{
	do
	{
		switch( ev )
		{
		case WM_GLB_VOIP_TO_UI_SND_MSG:
			{
				aot::sound::isound_card_msg* msg = reinterpret_cast<aot::sound::isound_card_msg*>(wparam);
				if ( msg == NULL )
				{
					return 0;
				}

				switch ( msg->msg_type )
				{
				case aot::sound::e_snd_msg_speaker_changed:
					if ( msg->msg_name )
					{
						this->voip_svc_impl_->set_wave_out_dev(msg->msg_name->c_str());
					}
					break;
				case aot::sound::e_snd_msg_mic_changed:
					if ( msg->msg_name )
					{
						this->voip_svc_impl_->set_wave_in_dev(msg->msg_name->c_str());
					}
					break;
				case aot::sound::e_snd_msg_agc_enable:
					this->voip_svc_impl_->agc_enable(msg->msg_other);
				    break;
				default:
				    break;
				}
				msg->destroy();
				return 0;
			}	
			break;
		default:
			break;
		}
	}while(0);

	if( sep )
	{
		if( sep->wparam ) sep->wparam->destroy();
		if( sep->lparam ) sep->lparam->destroy();	
	}
	return 0;
}
